define(function(require,exports,module){
    require ("jquery");
    (function($){
        /**
         *
         * @type {{
         * play: boolean 是否自动播放,
         * pauseTime: number 停留时间 毫秒,
         * animSpeed: number 切换速度 毫秒,
         * directionNav: boolean 是否使用左右按钮导航,
         * controlNav: boolean 是否使用导航控制条,
         * pageCount: number 每页显示的个数,
         * moveCount: number 每次移动的个数,
         * moveDrift: number 每个移动的距离
         * arrow:0  0表模向 1表竖向
         * }}
         */
        var defaults = {
            play: true,
            pauseTime: 3000,
            animSpeed: 500,
            directionNav: true,
            controlNav: true,
            prevText: '&#xe664;',
            nextText: '&#xe665;',
            pageCount: 0,
            moveCount: 1,
            moveDrift: 0,
            arrow:0
        };
        var options = $.extend({},defaults,options);
        $.fn.slider= function(options){
            //是否存在多个调用
            if(this.length == 0) return this;
            if(this.length > 1){
                this.each(function(){$(this).slider(options)});
                return this;
            }
            var elem = this,
                plugin = {};
            plugin.elem = this;
            var init = function(){
                var opts = $.extend({}, defaults, options);
                var slides = elem.find('.box'),
                    slider_prev = null,
                    slider_next = null,
                    slider_page = null,
                    slider_pages = null;

                var current = 0,
                    size = slides.children().size(),
                    maxDrift = (size - opts.pageCount) * opts.moveDrift * -1,
                    maxPage = Math.ceil((size - opts.pageCount) / opts.moveCount);
                var time = null;
                var move = function (delay) {
                    clearTimeout(time);
                    time = setTimeout(function () {
                        if (typeof(delay) == "undefined") {
                            current++;
                        }
                        if (current == maxPage) {
                            current = 0;
                        }

                        if (opts.directionNav) {
                            slider_prev.addClass('z-dis');
                            slider_next.addClass('z-dis');
                            if (current > 0) {
                                slider_prev.removeClass('z-dis');
                            }
                            if (current < maxPage - 1) {
                                slider_next.removeClass('z-dis');
                            }
                        }

                        if (opts.controlNav) {
                            slider_pages.removeClass('z-sel').eq(current).addClass('z-sel');
                        }
                        var drift = current * opts.moveCount * opts.moveDrift * -1;
                        drift = Math.abs(drift) > Math.abs(maxDrift) ? maxDrift : drift;
                        if(opts.arrow){
                            slides.stop(true, true).animate({
                                "marginTop": drift
                            }, opts.animSpeed, function () {
                                if (opts.play) {
                                    move();
                                }
                            });
                        }else{
                            slides.stop(true, true).animate({
                                "marginLeft": drift
                            }, opts.animSpeed, function () {
                                if (opts.play) {
                                    move();
                                }
                            });
                        }
                    }, delay || opts.pauseTime);
                };

                if (opts.controlNav) {
                    var oPage =$('<ul class="slider-page"></ul>');
                    var slider_page = "";
                    for(var i =0 ; i<maxPage;i++){
                        if(i==0){
                            slider_page +=  '<li data-index="'+i+'" class="z-sel">'+(i+1)+'</li>';
                        }else{
                            slider_page +=  '<li data-index="'+i+'">'+(i+1)+'</li>';
                        }
                    };
                    oPage.append(slider_page);
                    elem.append(oPage);
                    slider_pages = oPage.children();
                    slider_pages.on('mouseenter', function () {
                        current = this.getAttribute('data-index');
                        move(10);
                    });
                }

                if (opts.directionNav) {
                    slider_prev = $('<a class="slider-prev icon-font z-dis" href="javascript:;">' + opts.prevText + '</a>');
                    slider_next = $('<a class="slider-next icon-font z-dis" href="javascript:;">' + opts.nextText + '</a>');
                    elem.append(slider_prev).append(slider_next);
                    if (maxPage > 1) {
                        slider_next.removeClass('z-dis');
                    }

                    slider_prev.on('click', function () {
                        if (current > 0) {
                            current--;
                            move(10);
                        }
                    });

                    slider_next.on('click', function () {
                        if (current < maxPage - 1) {
                            current++;
                            move(10);
                        }
                    });
                }

                if (opts.play) {
                    move();
                }
            }
            init();
        };
    })(jQuery);
});